#ifndef _WIN32_CACHE_MANAGER_H
#define _WIN32_CACHE_MANAGER_H

#include "../LibCache.h"
#include <pthread.h>
#include <string>
#include <hash_map>
using namespace std;
using namespace stdext;

LIB_CACHE_NAMESPACE_BEGIN

class Win32CacheEntry;
class Win32CacheManager : public ICacheManager
{
public:
	Win32CacheManager(void);
	~Win32CacheManager(void);


	virtual bool Initialize(const char* cacheBaseDir = NULL,
							bool checkHash = true, 
							const size_t& maxMemSize = DEFAULT_MEM_CACHE_SIZE,
							const size_t& maxFolderSize = DEFAULT_FOLDER_CACHE_SIZE,
							const size_t& maxCacheEntrySize = DEFAULT_CACHE_ENTRY_SZIE);

	virtual bool Destroy();

	virtual bool Refresh();

	virtual const char* GetCacheBaseDir() const;

	virtual bool GetCache(const char* name, 
						ICacheEntry** cacheEntry = NULL, 
						bool checkDisk = true, 
						bool checkHash = true) const;

	virtual bool AddCache(const char* name, 
						const ICacheEntry* cacheEntry, 
						bool writeToDisk = true,
						bool checkHash = true);

	virtual bool AddCache(const char* name, 
						const byte* data, 
						const size_t size,
						ICacheEntry** cacheEntry = NULL,
						bool writeToDisk = true,
						bool checkHash = true);

	virtual bool RemoveCache(const char* name, bool removeDiskCopy = false);

	virtual bool CreateCacheEntry(const char* name, ICacheEntry** cacheEntry) const;
	
	virtual bool ReleaseCacheEntry(ICacheEntry* cacheEntry) const;

	virtual bool Lock(bool read = true) const;

	virtual bool UnLock() const;

	virtual bool TryLock(bool read = true) const;

private:
	ICacheEntry* AddCacheEntry(const string& name, bool checkHash);
	void ReleaseEntry();
	bool LoadEntry();

private:
	size_t m_maxFolderSize;
	size_t m_maxMemSize;
	size_t m_maxEntrySize;
	size_t m_currentCacheSize;
	size_t m_currentFolderSize;

	string m_baseDir;
	bool m_checkHash;
	pthread_rwlock_t* m_rwlock;
	typedef hash_map<string, ICacheEntry*> tCacheMap;
	tCacheMap m_cacheMap;
};

LIB_CACHE_NAMESPACE_END

#endif //_WIN32_CACHE_MANAGER_H